'''
Created on 2012-10-11

@author: Alexandre
'''
from tkinter import*
import Engin
import threading
import time

class Fenetre(Tk):
    largeurCase = 15
    temps = 75
    long = 40; haut = 30
    def __init__(self):
        super().__init__()
        self.jeu = Engin.Grille()
        self.jeu.setDimensions(Fenetre.haut, Fenetre.long)
        self.animation = False
        self.initialiserCanvas()
        self.b1 = Button(self, text="Play", command=self.animer)
        self.b2 = Button(self, text="Stop", command=self.stopper)
        self.b3 = Button(self, text="Canon!", command=self.genererCanon)
        self.b4 = Button(self, text="Effacer", command=self.effacer)
        self.b1.pack()
        self.b2.pack()
        self.b3.pack()
        self.b4.pack()
        #self.animThread = animationThread(self)
        #self.animThread.start()
        self.protocol("WM_DELETE_WINDOW", self.fermer)
        
        
    def initialiserCanvas(self):
        self.rectangles = {}
        self.grille = Canvas(self, width=Fenetre.largeurCase*Fenetre.long, height=Fenetre.largeurCase*Fenetre.haut)
        self.dessinerCanvas()
        self.grille.bind("<Button-1>", self.click)
        self.grille.pack()
        
    def dessinerCanvas(self):
        for i in range(self.jeu.dimensionX):
            for j in range(self.jeu.dimensionY):
                self.rectangles[i, j] = self.grille.create_rectangle(Fenetre.largeurCase*j, Fenetre.largeurCase*i,\
                                         Fenetre.largeurCase*j+Fenetre.largeurCase,\
                                         Fenetre.largeurCase*i+Fenetre.largeurCase, fill=self.definirCouleur(i, j),)
    def definirCouleur(self, i, j):
        if self.jeu.grilleCase[i][j].valeur:
            return 'black'
        else:
            return 'blue' 
    def rafraichirGrille(self):
        self.jeu.prochaineGrille()
        for c in self.jeu.changement:
            rectangle_id = self.rectangles[c]
            self.grille.itemconfig(rectangle_id, fill=self.definirCouleur(c[0], c[1]))
        if self.animation:
            self.after(Fenetre.temps, lambda: self.rafraichirGrille())
            
    def rafraichirComplet(self):
        for i in range(Fenetre.haut):
            for j in range(Fenetre.long):
                id = self.rectangles[i, j]
                self.grille.itemconfig(id, fill=self.definirCouleur(i, j))
    def click(self, event):
        i = event.y//Fenetre.largeurCase
        j = event.x//Fenetre.largeurCase
        self.jeu.inverserCase(i, j)
        rectangle_id = self.rectangles[i, j]
        self.grille.itemconfig(rectangle_id, fill=self.definirCouleur(i, j))
        
    def animer(self):
        if not self.animation:
            self.animation = True
            self.rafraichirGrille()
    def stopper(self):
        self.animation = False
    def genererCanon(self):
        self.jeu.genererCanon()
        self.rafraichirComplet()
    def effacer(self):
        self.jeu.effacer()
        self.rafraichirComplet()
    def fermer(self):
        self.animation = False
        self.destroy()
        
"""    
----------CLASSE OBSELETTE----------------
class animationThread(threading.Thread):
    def __init__(self, f):
        self.f = f
        threading.Thread.__init__(self)
    def run(self):
        while True:
            if self.f.animation:
                self.f.jeu.prochaineGrille()
                self.f.rafraichirGrille()
                time.sleep(Fenetre.temps)
------------------------------------------
"""    
if __name__ == "__main__":
    fenetre = Fenetre()
    fenetre.mainloop()
    